package com.ewanghuiju.app.ui.my.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.RedBeanHomeContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.RedBeanDetailsResponBean;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.WaterDetailsResponBean;
import com.ewanghuiju.app.model.dp.RealmHelper;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.my.RedBeanHomePresenter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import java.util.List;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 水滴/分红豆转增页面
 * @time 2020/2/19
 */
public class WaterAndRedbeanIncreaseActivity extends BaseActivity<RedBeanHomePresenter> implements RedBeanHomeContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_shares_num)
    EditText etSharesNum;
    @BindView(R.id.et_red_bean_num)
    EditText etRedBeanNum;
    @BindView(R.id.tv_number_text)
    TextView tvNumberText;
    @BindView(R.id.tv_service_charge)
    TextView tvServiceCharge;
    @BindView(R.id.tv_receive_lab)
    TextView tvReceiveLab;
    @BindView(R.id.tv_number_lab)
    TextView tvNumberLab;
    @BindView(R.id.tv_sure_increase)
    TextView tvSureIncrease;
    @BindView(R.id.tv_tip_text)
    TextView tvTipText;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_water_sum)
    TextView tvWaterSum;
    @BindView(R.id.layout_water_number)
    LinearLayout layoutWaterNumber;
    @BindView(R.id.tv_water_number)
    TextView tvWaterNumber;
    @BindView(R.id.tv_water_number_lab)
    TextView tvWaterNumberLab;

    private String numberText;
    private int mType;
    private float waterPoundage;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_water_redbean_increase;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.WATER_REDBEAN_TYPE, 1);
        numberText = getIntent().getStringExtra(Constants.WATER_INCREASE_NUMBER);
        if (mType == 1) {
            RealmHelper realmHelper = App.getAppComponent().realmHelper();
            UserInfoResponBean userInfoResponBean = realmHelper.getUserInfoResponBean();
            layoutWaterNumber.setVisibility(View.VISIBLE);
            tvTitle.setText("加权分红");
            tvAll.setText("当前可用分红豆" + (TextUtils.isEmpty(numberText) ? "0" : numberText));
            tvAll.setTextColor(ContextCompat.getColor(mContext, R.color.color_666));
            etRedBeanNum.setHint("系统自动计算");
            tvWaterNumber.setHint("系统自动计算");
            etRedBeanNum.setFocusable(false);
            tvWaterNumber.setFocusable(false);
            if (userInfoResponBean != null) {
                tvWaterSum.setText("当前可用水滴" + (TextUtils.isEmpty(userInfoResponBean.getIntegral()) ? "0" : userInfoResponBean.getIntegral()));
            }
            etSharesNum.setHint("请输入转化加权分红股数");
            etSharesNum.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    etRedBeanNum.setText("");
                    tvWaterNumber.setText("");
                    if (!TextUtils.isEmpty(s.toString())) {
                        //获取信息
                        mPresenter.ptiredeanProportion(s.toString());
                    }
                }
            });

            mPresenter.getPtiguize("5");
        } else if (mType == 2) {
            waterPoundage = getIntent().getFloatExtra(Constants.WATER_INCREASE_POUNDAGE, 0);
            tvWaterNumberLab.setText("手续费");
            tvWaterNumber.setHint("系统自动计算");
            tvWaterNumber.setFocusable(false);

            tvWaterSum.setText("* 手续费"+waterPoundage+"%，自动计算");
            tvServiceCharge.setVisibility(View.VISIBLE);
            tvTitle.setText("水滴转赠");
            tvTipText.setText(getString(R.string.popup_tip_water_content));
            tvReceiveLab.setText("接收方账号");
            tvNumberLab.setText("转赠数量");
            tvSureIncrease.setText("确认转赠");
            etSharesNum.setHint("请输入接收人e网惠聚账号");
            etRedBeanNum.setHint("请输入转赠数量最少1滴起");
            tvNumberText.setText("当前可用水滴" + (TextUtils.isEmpty(numberText) ? "0" : numberText));
            etRedBeanNum.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        tvWaterNumber.setText(StringUtil.getFloatByString(s.toString())*waterPoundage/100+"");
                    }else{
                        tvWaterNumber.setText("");
                    }
                }
            });

        }

    }


    @OnClick({R.id.tv_sure_increase, R.id.tv_all})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {
            //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_sure_increase:
                //转增
                String etSharesNumStr = etSharesNum.getText().toString();
                String etRedBeanNumStr = etRedBeanNum.getText().toString();
                if (mType == 1) {
                    if (TextUtils.isEmpty(etSharesNumStr)) {
                        StyleableToast.makeText(mContext, "请输入转化加权分红股数", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    LoadingDialogUtils.show(mContext);
                    mPresenter.increase(etSharesNumStr, "");
                } else if (mType == 2) {
                    if (TextUtils.isEmpty(etSharesNumStr)) {
                        StyleableToast.makeText(mContext, "请输入接收人e网惠聚账号", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    if (TextUtils.isEmpty(etRedBeanNumStr)) {
                        StyleableToast.makeText(mContext, "请输入转赠数量最少1滴起", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    if (!StringUtil.isPhone(etSharesNumStr)) {
                        StyleableToast.makeText(mContext, "请输入正确的账号", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    LoadingDialogUtils.show(mContext);
                    mPresenter.ptitransferIntegral(etRedBeanNumStr, etSharesNumStr);
                }
                break;
            case R.id.tv_all:
                //全部转化
                if (TextUtils.isEmpty(numberText)) {
                    StyleableToast.makeText(mContext, "没有额度", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                etRedBeanNum.setText(numberText);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(mToolbar)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void intSmartRefreshLayout() {
        super.intSmartRefreshLayout();
    }

    @Override
    public void showContent(RedBeanDetailsResponBean bean) {

    }

    @Override
    public void showContentError() {
    }

    @Override
    public void increaseSuccess() {
        //转增成功
        LoadingDialogUtils.dismissDialog_ios();
        if (mType == 1) {
            StyleableToast.makeText(mContext, "转化成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
            onBackPressedSupport();
        } else if (mType == 2) {
            StyleableToast.makeText(mContext, "转赠成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
            onBackPressedSupport();
        }
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_WATER_REDBEAN));
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
    }

    @Override
    public void showWaterDetails(WaterDetailsResponBean bean) {

    }

    @Override
    public void ptiredeanProportion(BaseResponse bean) {
        //转化股数成功
        LoadingDialogUtils.dismissDialog_ios();
        if (bean == null) {
            return;
        }
        etRedBeanNum.setText(TextUtils.isEmpty(bean.getStocks()) ? "" : bean.getStocks());
        tvWaterNumber.setText(TextUtils.isEmpty(bean.getIntegral()) ? "" : bean.getIntegral());
    }

    @Override
    public void showWaterContent(List<RedBeanDetailsResponBean> list) {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void showPtiguize(RuleBean bean) {
        //获取规则成功
        if (bean == null) {
            return;
        }
        if (mType == 1) {
            if (!TextUtils.isEmpty(bean.getContent())) {
                tvTipText.setText(bean.getContent());
            }
        }
    }
}
